import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:tracker/pages/home/phone_book/model/contact_list_resp.dart';
import 'package:tracker/pages/home/phone_book/repository/phone_book_repository.dart';
import 'package:tracker/pages/home/phone_book/state/phone_book_state.dart';
import 'package:tracker/provider/global_provider.dart';
import 'package:tracker/utils/toast_utils.dart';

part 'phone_book_controller.g.dart';

@riverpod
class PhoneBookController extends _$PhoneBookController {
  @override
  FutureOr<PhoneBookState> build() {
    return const PhoneBookState(contactList: []);
  }

  /// 获取联系人列表
  Future<void> getContactList() async {
    state = const AsyncValue.loading();
    var deviceInfo = ref.read(deviceInfoProvider);
    var contactList = await ref.read(phoneBookRepositoryProvider).getContactList(imei: deviceInfo.imei!);
    state = AsyncValue.data(state.value!.copyWith(contactList: contactList));
  }

  /// 添加联系人
  Future<void> addContact(String name, String phone) async {
    // 更新列表
    final contactList = List<Contact>.from(state.value!.contactList);
    var contact = Contact()
      ..name = name
      ..phone = phone;
    contactList.add(contact);
    state = AsyncValue.data(state.value!.copyWith(contactList: contactList));
  }

  /// 修改联系人
  Future<void> updateContact(int index, String name, String phone) async {
    // 更新列表
    final contactList = List<Contact>.from(state.value!.contactList);
    var contact = contactList[index];
    contact.name = name;
    contact.phone = phone;
    contactList[index] = contact;
    state = AsyncValue.data(state.value!.copyWith(contactList: contactList));
  }

  /// 删除联系人
  Future<void> deleteContact(int index) async {
    // 更新列表
    final contactList = List<Contact>.from(state.value!.contactList);
    contactList.removeAt(index);
    state = AsyncValue.data(state.value!.copyWith(contactList: contactList));
  }

  /// 上移联系人
  Future<void> moveUpContact(int index) async {
    // 第一个时不能上移
    if (index == 0) {
      ToastUtils.showToast('已经是第一个了');
      return;
    }
    final contactList = List<Contact>.from(state.value!.contactList);
    // 将当前联系人与上一个联系人交换位置
    final temp = contactList[index];
    contactList[index] = contactList[index - 1];
    contactList[index - 1] = temp;
    state = AsyncValue.data(state.value!.copyWith(contactList: contactList));
  }

  /// 下移联系人
  Future<void> moveDownContact(int index) async {
    // 最后一个时不能下移
    if (index == state.value!.contactList.length - 1) {
      ToastUtils.showToast('已经是最后一个了');
      return;
    }
    final contactList = List<Contact>.from(state.value!.contactList);
    // 将当前联系人与下一个联系人交换位置
    final temp = contactList[index];
    contactList[index] = contactList[index + 1];
    contactList[index + 1] = temp;
    state = AsyncValue.data(state.value!.copyWith(contactList: contactList));
  }

  /// 提交联系人
  Future<void> submit() async {
    ToastUtils.showLoading(status: '提交中');
    var deviceInfo = ref.read(deviceInfoProvider);
    var contactList = state.value!.contactList;
    var res = await ref.read(phoneBookRepositoryProvider).submit(imei: deviceInfo.imei!, contactList: contactList);
    ToastUtils.dismissLoading();
    if (res.code == 200) {
      ToastUtils.showSuccess('提交成功');
      getContactList();
    } else {
      ToastUtils.showToast(res.msg ?? '提交失败');
    }
  }
}
